<?php
/**
 * @Author: king
 * @Last Modified by:   king
 * Date: 2018/6/6
 * Time: 17:38
 */

namespace App\Models\Api;
use App\Exceptions\ApiException;
use App\Models\Api\Base;
use App\Models\Api\Goods;
use Illuminate\Database\Eloquent\Model;
use Cache;
class Map extends Base
{
    protected $table = 'map';

     /**
     * 检测该经纬度用户是否已经收藏
     * @param array $map
     * latitude float must 纬度
     * longitude float must 经度
     * uid int must 用户id
     * rid int must 档案id
     * @return array
     */
    public function checkCollectStatus($map = [])
    {
        $result = [];
        try
        {
            $data = $this->from('map as m')
                ->join('map_collect as mc', 'mc.mid','=','m.id')
                ->where([
                    'm.latitude' => $map['latitude'],
                    'm.longitude' => $map['longitude']
                ])
//                ->whereBetween('m.latitude', '')
                ->where(['mc.uid' => $map['uid'], 'mc.rid' => $map['rid']])
                ->firstOrFail();
            $result['is_collect'] = true;
            $result['collect_id'] = $data['id'];
        }
        catch (\Exception $e)
        {
            $result['is_collect'] = false;
            $result['collect_id'] = 0;
        }
        return $result;
    }

    /**
     * 检测档案已解锁的地图线条或命运之城
     * @param array $map
     * uid int must 用户id
     * rid int must 档案id
     * isself int must 是否为自己的档案
     * @return string '111111111110'; 长度为12位字符串
     * 日,月,水,金,火,木,土,天,海,冥,北交,命运之城
     * 0表示未解锁 1表示已解锁
     * @throws \app\common\lib\exception\ApiException
     */
    public function getRecodeUnlockLines($map = [])
    {
        $vipLevel = getVip($map['uid']);
        // 特殊免解锁用户，全部档案解锁
        if ($vipLevel === 1 || ($vipLevel > 1 && $map['isself'] == 1)) return '111111111111';

        $data = false;

        if (intval($this->cache_on) >= 1 && Cache::has('recode_lines_' . $map['rid'])) $data = Cache::get('recode_lines_' . $map['rid']);

        if ($data === false)
        {

            $data = '000000011000';// 默认 开启天海

            $acg_goods = (new Goods())->getGoodsForACG($map);
            $gids = $orders = [];
            if (!empty($acg_goods)) foreach ($acg_goods as $ag) $gids[] = $ag['id'];

            //获取当前档案已购买的命格线id
            try
            {
                $orders = $this->table('orders_goods as og')
                    ->select('og.gid')
                    ->join('orders as o', 'og.oid','=','o.id', 'LEFT')
                    ->where(['o.pay_time','>', 0], 'uid',$map['uid'], 'rid',$map['rid'])
                    ->whereIn('og.gid',$gids)
                    ->orderBy('o.pay_time', 'DESC')
                    ->get();

                foreach ($orders as $order)
                {
                    // 打包解锁表示全解锁
                    if ($order['gid'] == 19)
                    {
                        $data = '111111111111';
                        break;
                    } else
                    {
                        $data[$order['gid'] - 7] = 1;// 太阳线起始gid为7
                    }
                }
            }
            catch (\Exception $e)
            {

            }

            // 未全部解锁 且 查看自己的档案时 ，查看是否有赠送
            if ($data != '111111111111' && isset($map['isself']) && $map['isself'] == 1)
            {

                try
                {
                    $award_acg = $this->table('award_goods')
                        ->select('agid','auid')
                        ->where(['auid' => $map['uid']])
                        ->whereIn('agid',$gids)
                        ->get();
                    foreach ($award_acg as $award)
                    {
                        // 打包解锁  表示全解锁
//                        if ($award['gid'] == 19) {
//                            $data = '111111111111';
//                            break;
//                        } else {
//                            $data[$award['gid'] - 7] = 1;// 太阳线起始gid为7
//                        }

                        $data[$award['agid'] - 7] = 1;// 太阳线起始gid为7

                    }
                }
                catch (\Exception $e)
                {

                }
            }
            Cache::set('recode_lines_' . $map['rid'], $data, 86400 * 3);
        }

        return $data;
    }
}